const http = require('node:http');
const { URL, URLSearchParams } = require('node:url');

const port = process.argv[2];

const server = http.createServer(function (req, res) {
  const pathname = req.url.split('?')[0];
  const search = req.url.split('?')[1];
  const params = new URLSearchParams(search);
  const times = new Date(params.get('iso'));

  if (req.method !== "GET") {
    res.statusCode = 405;
    res.end("Method Not Allowed");
  } else {
    if (pathname.includes("/api/parsetime")) {
      let json = JSON.stringify({
        hour: times.getHours(),
        minute: times.getMinutes(),
        second: times.getSeconds()
      })
      res.end(json)
    }
    else if (pathname.includes("/api/unixtime")) {
      let json = JSON.stringify({
        unixtime: times.getTime()
      })
      res.end(json)

    } else {
      res.statusCode = 404;
      res.end("Not Found");
    }

  }

});

server.listen(port)
